var User = require('../model_proxy').User;
var logger = require('../logger');

var mod = {
    login: function(req, res) {
        var userInfo = req.body;
        console.log('userInfo---', userInfo);
        logger.error('type::', typeof(userInfo));
        console.log('userInfo-username---', userInfo.username);
        User.login({
            userName: userInfo.username,
            password: userInfo.password
        }, function(err, data) {
            console.log('err:::', err);
            console.log('data::::', data);
            if(err) {
                console.error('login error::', err);
            }
            
            if(data.length != 0) {
                req.session.user= data[0];
                logger.info('用户：' + data[0].userName + '(' + data[0].role + ')登录成功！');
                res.json({
                    status: 'success',
                    data: data[0],
                    message: '登录成功！'
                });
            } else {
                res.json({
                    status: 'fail',
                    message: '登录失败！'
                });
            }
            
        })
    },

    getUser: function(req, res) {
        const obj = {};
        User.getUser(obj, function(err, data) {
            console.log('getuser---', data);
            if(err) {

            } else {
                res.json({
                    status: 'success',
                    data: data,
                });
            }
            
        });
    },

    // 查询非好友列表
    getUnFriend: function(req, res) {

        User.getUnFriend(req.session.user._id, function(err, data) {
            console.log('getUnFriend--', data);
            res.json({
                status: 'success',
                data: data
            });
        });
    },

    getFriendList: function(req, res) {
        User.getFriendList(req.session.user._id, function(err, data) {
            console.log('getFriendList--', data);
            res.json({
                status: 'success',
                data: data
            });
        });
    },

    addFriend: function(req, res) {
        const option = req.body;
        option.userId = req.session.user._id;
        logger.error('addfriend--option===', option);
        User.addFriend(option, function(err, data) {
            console.log('data---', data);
            res.json({
                status: 'success',
            });
        });
    },

    delete: function(req, res) {
        var userId = req.body.userId;
        console.log('userId--', userId);
        User.delete(userId, function(err, data) {
            if(err) {

            } else {
                console.log('delete:::', data);
                res.json({
                    status: 'success',
                });
            }
        });
        
    },

    addUser: function(req, res) {
        var userInfo = req.body;
        User.addUser(userInfo, function(err, data) {
            if(err) {
                console.error('err:;', err);
            } else {
                console.log('data--', data);
                res.json({
                    status: 'success',
                    data: data
                });
            }
        });
    },

    register: function() {
        User.register();
    }
}

module.exports = mod;